package com.expense.app.admin.services.impl;

import cn.hutool.core.convert.Convert;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.adminMenu.AdminMenuDetailDTO;
import com.expense.app.admin.model.dto.adminMenu.AdminMenuListDTO;
import com.expense.app.admin.model.params.adminMenu.AdminMenuDetailParam;
import com.expense.app.admin.services.AdminMenuServ;
import com.expense.common.dao.auth.AdminMenuDao;
import com.expense.common.entity.params.DetailParam;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.auth.AdminMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminMenuServImpl extends BaseService implements AdminMenuServ {

    @Autowired
    AdminMenuDao adminMenuDao;

    @Override
    public AdminMenuListDTO findList() {
        List<AdminMenu> menuList =  adminMenuDao.findList();
        List<AdminMenu> result = AdminMenu.generateListTree(menuList,"0");
//        List<AdminMenu> result = getTreeDataList(menuList);
        AdminMenuListDTO dto = new AdminMenuListDTO();
        dto.setDataList(result);
        return dto;
    }

    private List<AdminMenu> getTreeDataList(List<AdminMenu> menuList) {

        List<AdminMenu> adminMenuList = new ArrayList<>();
        Map<String, AdminMenu> map = new HashMap<>();
        for (AdminMenu menu : menuList) {
            map.put(menu.getCode(), menu);
        }
        for (AdminMenu menu : menuList) {
            if (menu.getLevel() == 1) {
                adminMenuList.add(map.get(menu.getCode()));
            }
            if (menu.getParentCode() != null && map.get(menu.getParentCode()) != null) {
                AdminMenu parent = map.get(menu.getParentCode());
                if (parent.getChildren() == null) parent.setChildren(new ArrayList<AdminMenu>());
                parent.getChildren().add(menu);
                map.put(parent.getCode(), parent);
            }
        }
        return adminMenuList;
    }
    @Override
    public AdminMenuDetailDTO findDetail(DetailParam param){
        Long id = Convert.toLong(param.getId(),0l);
        if(id <= 0) throw new PlatformException("未获取到菜单信息");
        AdminMenu menu = adminMenuDao.findByIdAndDeleted(id);
        return  new AdminMenuDetailDTO(menu) ;
    }

    @Override
    public AdminMenuDetailDTO saveOne(AdminMenuDetailParam param){
        AdminMenu menu = param.getAdminMenu();
        setBaseInfo(menu);
        if(Convert.toLong(menu.getId(),0l) <= 0){
            // 如果父亲是叶子节点，将父亲设置为非叶子节点
            AdminMenu parent = adminMenuDao.findByCode(menu.getCode());
            if(parent !=null &&(parent.getIsLeaf() == null || parent.getIsLeaf() == 1)){
                parent.setIsLeaf(0);
                adminMenuDao.updateById(parent);
            }
            menu.setIsLeaf(1);
            adminMenuDao.insert(menu, true);
            menu.setShowOrder(menu.getId());
            adminMenuDao.updateTemplateById(menu);
        }
        // 新增时，为Code 赋值
        if(menu.getCode() == null) menu.setCode(Convert.toStr(menu.getId()));

        adminMenuDao.saveOne(menu);

        return new AdminMenuDetailDTO(menu);
    }

    @Override
    public Boolean deleted(DetailParam param){
        AdminMenu menu = adminMenuDao.findByIdAndDeleted(param.getId());
        adminMenuDao.deleteById(param.getId());
        //删除之后，判断父亲节点是否还有子节点，如果没有，则将父节点改为叶子节点
        List<AdminMenu> children = adminMenuDao.loadChildren(menu.getParentCode());
        if(children== null || children.size() == 0){
            AdminMenu parent = adminMenuDao.findByCode(menu.getParentCode());
            if(parent != null){
                parent.setIsLeaf(1);
                adminMenuDao.updateById(parent);
            }
        }
        return  true;
    }

}
